package com.uit.ecommerce.servlets;

import com.uit.ecommerce.bo.AreaBO;
import com.uit.ecommerce.bo.WeatherAreaBO;
import com.uit.ecommerce.dto.AreaDTO;
import com.uit.ecommerce.dto.WeatherAreaDTO;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 *
 * @author ngloc_it
 */
@WebServlet(name="UpdateWeatherArea", urlPatterns={"/UpdateWeatherArea"})
public class UpdateWeatherArea extends HttpServlet {
   
  

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
       
    } 

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        
    }

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        super.service(req, resp);
        req.setCharacterEncoding("UTF-8");
        resp.setContentType("text/html;charset=UTF-8");
        HttpSession session = req.getSession();
        
        String function = (String)req.getParameter("function");
        String area = (String) req.getParameter("area");
        session.setAttribute("areaname", area);

        if(function.equalsIgnoreCase("submit")){
            try {
                submitWeatherArea(req, resp);
            } catch (Exception ex) {
                Logger.getLogger(UpdateWeatherArea.class.getName()).log(Level.SEVERE, null, ex);
            }
            return;
        }

        String path = "./jsps/CapNhatThoiTietKhuVuc.jsp";
        resp.sendRedirect(path);
    }

    private void submitWeatherArea(HttpServletRequest req, HttpServletResponse resp) throws Exception{
        String report = "Thêm thành công";
        PrintWriter out = resp.getWriter();
        try {
            WeatherAreaBO waB = new WeatherAreaBO();
            String areaName = (String) req.getParameter("area");
            String date = (String) req.getParameter("date");
            String tempMin = (String) req.getParameter("tempMin");
            String tempMax = (String) req.getParameter("tempMax");
            String info = (String) req.getParameter("info");
            String status = (String) req.getParameter("status");
            
            AreaBO areaBo = new AreaBO();
            AreaDTO areaDto = areaBo.getAreaByName(areaName);           
            
            WeatherAreaDTO wad = new WeatherAreaDTO(areaDto.getId(), 
                                                    areaName, 
                                                    date, 
                                                    Float.parseFloat(tempMin),
                                                    Float.parseFloat(tempMax), 
                                                    info,
                                                    status);
            if(waB.checkWeatherAreaExit(wad) == true){
                report = "Bản tin thời tiết cho khu vực " + areaName + " ngày " + date + " đã có";
            }else{
                waB.insertWeatherArea(wad);
            }
        }catch (IOException ex) {
            report = ex.toString();
        }
        out.println(report);
    }

}
